import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

public class RegisterFrame extends JFrame implements ActionListener,  RegisterField, KeyListener {
    //隐藏容器
    Container container = getContentPane();
    //验证码
    String code = Util.code();
    //密码;
    String visiblePassword = "";
    boolean showPassword;


    //初始化对象
    public  RegisterFrame() {
        initFrame();
        initWord();
        initButton();
        setVisible(true);
        // 添加背景图片
        background.setBounds(40, 40, 508, 560);
        container.add(background);
    }

    //初始化按钮
    public void initButton() {
        //添加按钮
        //注册
        registerJButton.setBounds(250, 500, 120, 47);
        registerJButton.setBorder(new BevelBorder(5));
        registerJButton.addActionListener(this);
        container.add(registerJButton);
        registerJButton.setVisible(true);
        //验证码
        codeJButton.setText(code);
        codeJButton.setBounds(300, 350, 100, 50);
        codeJButton.addActionListener(this);
        codeJButton.setVisible(true);
        container.add(codeJButton);
        //显示密码
        showPasswordJButton.setBounds(400, 250, 50, 50);
        showPasswordJButton.addActionListener(this);
        showPasswordJButton.setVisible(true);
        container.add(showPasswordJButton);
    }

    //初始化文本框和文字
    public void initWord() {
        //添加文本框和文字
        //用户名
        userNameJLabel.setBounds(100, 150, 200, 50);
        container.add(userNameJLabel);
        userNameJtextField.setBounds(200, 150, 200, 50);
        container.add(userNameJtextField);
        userNameJtextField.setVisible(true);
        //密码
        passwordJLabel.setBounds(100, 250, 200, 50);
        container.add(passwordJLabel);
        passwordJTextField.setBounds(200, 250, 200, 50);
        passwordJTextField.addKeyListener(this);
        container.add(passwordJTextField);
        passwordJTextField.setVisible(true);
        //验证码
        codeJLabel.setBounds(100, 350, 200, 50);
        container.add(codeJLabel);
        codeJTextField.setBounds(200, 350, 100, 50);
        codeJTextField.addKeyListener(this);
        container.add(codeJTextField);
        codeJTextField.setVisible(true);
    }

    //初始化窗口
    public void initFrame() {

        setSize(600, 700);
        setLocationRelativeTo(null);
        setTitle("注册");
        //取消中间放置布局
        setLayout(null);
        addKeyListener(this);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    //初始化弹窗
    private static void initDialog(JLabel jLabel) {
        JDialog dialog = new JDialog();
        dialog.setSize(100, 100);
        dialog.getContentPane().add(jLabel);
        dialog.setLocationRelativeTo(null);
        dialog.setModal(true);
        dialog.setVisible(true);
    }

    //隐藏密码
    public void hidePassword() {
        String hide = "";
        for (int i = 0; i < visiblePassword.length(); i++) {
            hide += "*";
        }
        passwordJTextField.setText(hide);
    }

    //点击事件
    @Override
    public void actionPerformed(ActionEvent e) {
        Object obj = e.getSource();

        //注册
        if (obj == registerJButton) {
             register();
        }

        //验证码
        if (obj == codeJButton) {
            code = Util.code();
            codeJButton.setText(code);
        }

        //显示密码
        if (obj == showPasswordJButton) {

            if (!showPassword) {
                passwordJTextField.setText(visiblePassword);
                showPassword = true;
            } else {
                hidePassword();
                showPassword = false;
            }

        }
    }

    //初始化注册按钮
    private void register() {
        if (userNameJtextField.getText().equals("")) {
            initDialog(new JLabel("用户名不能为空"));
        } else if (passwordJTextField.getText().equals("")) {
            initDialog(new JLabel("密码不能为空"));
        } else if (!codeJTextField.getText().equalsIgnoreCase(code)) {
            initDialog(new JLabel("验证码错误"));
        } else {
             new LoginFrame();
            setVisible(false);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        Object obj = e.getSource();
        //密码区
        if (obj == passwordJTextField) {
            //密码删除
            if (keyCode == 8) {

                if (passwordJTextField.getText() == null) {
                    visiblePassword = "";
                    return;
                }

                if (visiblePassword.length() - 1 == 0) {
                    return;
                }

                if (visiblePassword.length() >= 2) {
                    String str = visiblePassword.substring(0, visiblePassword.length() - 1);
                    visiblePassword = str;
                }
            }
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        int keyCode = e.getKeyCode();
//        System.out.println(keyCode);
        Object obj = e.getSource();
        //验证码区
        if(obj==codeJTextField){
            if(keyCode==10)
                register();
        }
        //密码删除
        if (obj == passwordJTextField) {
            //禁用键
            int[]forbidKey={67,17,16,192,20,18,524,37,38,10};
            for(int x:forbidKey){
                if(x==keyCode)
                    return;
            }
            //DEL键
            if (keyCode == 8) {
                if (passwordJTextField.getText() == null) {
                    visiblePassword = "";
                    return;
                }

                if (visiblePassword.length() - 1 == 0) {
                    visiblePassword = "";
                    return;
                }

                if (visiblePassword.length() >= 2) {
                    String str = visiblePassword.substring(0, visiblePassword.length() - 1);
                    visiblePassword = str;
                }
                return;
            }
            //获取用户输入的字符
            visiblePassword += e.getKeyChar();
            //点击显示密码时不在隐藏密码
            if (!showPassword)
                hidePassword();
        }
    }
}
